package com.taorenqi.business.fragment.mine;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taorenqi.business.R;
import com.taorenqi.business.adapter.RechargeRecordVipAdapter;
import com.taorenqi.business.app.MyApp;
import com.taorenqi.business.fragment.base.BaseBackFragment;
import com.taorenqi.business.widget.statusview.StatusView;
import com.taorenqi.business.widget.subscribers.PageDataSubscriberOnNext;
import com.taorenqi.business.widget.subscribers.ProgressSubscriber;
import com.taorenqi.data.common.BasePageableResponse;
import com.taorenqi.data.entity.mine.RechargeRecordVipEntity;
import com.taorenqi.data.service.HttpRequest;
import com.taorenqi.data.service.MineDataHttpRequest;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;
import java.util.LinkedList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;

/**vip充值记录
 * A simple {@link Fragment} subclass.
 */
public class RechargeRecordVipFragment extends BaseBackFragment implements BaseQuickAdapter.RequestLoadMoreListener,SpringView.OnFreshListener{


    @Bind(R.id.status_view)
    StatusView statusView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.swipe_menu_recyclerview)
    SwipeMenuRecyclerView mSwipeMenuRecyclerview;
    @Bind(R.id.record_springview)
    SpringView mSpringview;

    private int page = 1;
    private int TOTAL_COUNTER = 0;
    private int mCurrentCounter = 0;
    private BasePageableResponse<RechargeRecordVipEntity> data;
    private PageDataSubscriberOnNext<BasePageableResponse<RechargeRecordVipEntity>> subscriber;
    private RechargeRecordVipAdapter mAdapter;
    private List<RechargeRecordVipEntity> list = new LinkedList<>();

    public static RechargeRecordVipFragment newInstance() {
        // Required empty public constructor
        RechargeRecordVipFragment fragment = new RechargeRecordVipFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recharge_record, container, false);
        ButterKnife.bind(this, view);
        initToolbar();
        initView();
        initAdapter();
        initHttpRequestData();
        return view;
    }


    private void initHttpRequestData() {
        subscriber = new PageDataSubscriberOnNext<BasePageableResponse<RechargeRecordVipEntity>>() {
            @Override
            public void onNext(BasePageableResponse<RechargeRecordVipEntity> entity) {
                data = entity;
                list = data.getDatalist();
                mSpringview.onFinishFreshAndLoad();

                if (mCurrentCounter == 0) {
                    mAdapter.setNewData(list);
                } else {
                    mAdapter.addData(list);
                }
                mCurrentCounter = mAdapter.getData().size();
                TOTAL_COUNTER = data.getCountData();
                Log.e("total",TOTAL_COUNTER + " ");
                mAdapter.loadMoreComplete();
            }

        };

        MineDataHttpRequest.getInstance(MyApp.getApp()).getVipRecharge(new ProgressSubscriber(subscriber, _mActivity),page, HttpRequest.DEFAULT_PAGE_SIZE);

    }



    private void initAdapter() {

        mAdapter = new RechargeRecordVipAdapter(list);
        mAdapter.openLoadAnimation();
        mAdapter.setEnableLoadMore(true);
        mAdapter.setOnLoadMoreListener(this);
        mSwipeMenuRecyclerview.setAdapter(mAdapter);
    }

    private void initView() {
        mSpringview.setType(SpringView.Type.FOLLOW);
        mSpringview.setHeader(new DefaultHeader(_mActivity));
        mSpringview.setListener(this);
        mSwipeMenuRecyclerview.setHasFixedSize(true);
        mSwipeMenuRecyclerview.addItemDecoration(getItemDecoration());
        mSwipeMenuRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mSwipeMenuRecyclerview.setSwipeMenuCreator(swipeMenuCreator);
        mSwipeMenuRecyclerview.setSwipeMenuItemClickListener(mMenuItemClickListener);
    }


    protected RecyclerView.ItemDecoration getItemDecoration() {
        return new DefaultItemDecoration(ContextCompat.getColor(_mActivity, R.color.myGrayText));
    }
    /**
     * 菜单创建器，在Item要创建菜单的时候调用。
     */
    private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width = getResources().getDimensionPixelSize(R.dimen.dp_70);

            // 1. MATCH_PARENT 自适应高度，保持和Item一样高;
            // 2. 指定具体的高，比如80;
            // 3. WRAP_CONTENT，自身高度，不推荐;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;


            // 添加右侧的，如果不添加，则右侧不会出现菜单。
            {
                SwipeMenuItem deleteItem = new SwipeMenuItem(_mActivity)
                        .setBackground(R.drawable.selector_red)
                        .setText("删除")
                        .setTextColor(Color.WHITE)
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(deleteItem);// 添加菜单到右侧。

            }
        }
    };

    /**
     * RecyclerView的Item的Menu点击监听。
     */
    private SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
        @Override
        public void onItemClick(SwipeMenuBridge menuBridge) {
            menuBridge.closeMenu();

            int direction = menuBridge.getDirection(); // 左侧还是右侧菜单。
            int adapterPosition = menuBridge.getAdapterPosition(); // RecyclerView的Item的position。
            int menuPosition = menuBridge.getPosition(); // 菜单在RecyclerView的Item中的Position。

            if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
                list.remove(adapterPosition);
                mAdapter.notifyDataSetChanged();
            } else if (direction == SwipeMenuRecyclerView.LEFT_DIRECTION) {
            }
        }
    };

    private void initToolbar() {
        toolbar.setTitle("Vip充值记录");
        initToolbarNav(toolbar);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        mCurrentCounter = 0;
        MineDataHttpRequest.getInstance(MyApp.getApp()).getVipRecharge(new ProgressSubscriber(subscriber, _mActivity),1, HttpRequest.DEFAULT_PAGE_SIZE);
        mAdapter.setEnableLoadMore(true);
        mAdapter.removeAllFooterView();
    }

    @Override
    public void onLoadmore() {

    }

    @Override
    public void onLoadMoreRequested() {
        mSwipeMenuRecyclerview.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mCurrentCounter >= TOTAL_COUNTER) {
                    mAdapter.loadMoreEnd();

                } else {
                    MineDataHttpRequest.getInstance(MyApp.getApp()).getVipRecharge(new ProgressSubscriber(subscriber, _mActivity),getCurrentPage(), HttpRequest.DEFAULT_PAGE_SIZE);
                }
            }
        }, 200);
    }

    public int getCurrentPage() {
        return mCurrentCounter / HttpRequest.DEFAULT_PAGE_SIZE + 1;
    }
}